<template>
  <a-card :bordered="false">
    <div class="card-title">{{ $route.meta.title }}</div>
    <div class="table-operator">
      <!-- 搜索板块 -->
      <a-row class="row-item-search">
        <a-form :form="searchForm" class="search-form" layout="inline" @submit="handleSearch">
          <a-form-item label="转账单号">
            <a-input v-decorator="['transferNo']" placeholder="请输入转账单号"/>
          </a-form-item>
          <a-form-item label="应用编号">
            <a-select v-decorator="['appId']" placeholder="请选择应用">
              <a-select-option v-for="item in appList" :key="item.id" :value="item.id">
                {{ item.name }}
              </a-select-option>
            </a-select>
          </a-form-item>
          <a-form-item label="转账渠道">
            <a-select v-decorator="['channelCode']" placeholder="请选择转账渠道">
              <a-select-option v-for="item in PayChannelCode.data" :key="item.key" :value="item.key">
                {{ item.name }}
              </a-select-option>
            </a-select>
          </a-form-item>
          <a-form-item label="商户单号">
            <a-input v-decorator="['merchantTransferId']" placeholder="请输入商户单号"/>
          </a-form-item>
          <a-form-item label="转账状态">
            <a-select v-decorator="['status']" placeholder="请选择转账状态">
              <a-select-option v-for="item in Status.data" :key="item.key" :value="item.key">
                {{ item.name }}
              </a-select-option>
            </a-select>
          </a-form-item>
          <a-form-item label="收款人姓名">
            <a-input v-decorator="['userName']" placeholder="请输入收款人姓名"/>
          </a-form-item>
          <a-form-item label="收款人账号">
            <a-input v-decorator="['userAccount']" placeholder="请输入收款人账号"/>
          </a-form-item>
          <a-form-item label="渠道单号">
            <a-input v-decorator="['channelTransferNo']" placeholder="请输入渠道单号"/>
          </a-form-item>
          <a-form-item label="创建时间">
            <a-range-picker
                v-decorator="['between']" format="YYYY-MM-DD HH:mm:ss" show-time
                value-format="YYYY-MM-DD HH:mm:ss"/>
          </a-form-item>
          <a-form-item class="search-btn">
            <a-button html-type="submit" icon="search" type="primary">搜索</a-button>
          </a-form-item>
          <a-form-item class="search-btn">
            <a-button @click="handleReset">重置</a-button>
          </a-form-item>
        </a-form>
      </a-row>
    </div>
    <s-table
        ref="table"
        :data="loadData"
        :loading="isLoading"
        :pageSize="15"
        :scroll="{ x: 1400, y: '420px', scrollToFirstRowOnChange: true }"
        rowKey="id"
    >
      <a-table-column key="creationTime" :width="180" data-index="creationTime" title="创建时间">
        <template #default="text">
          <span>{{ text | moment }}</span>
        </template>
      </a-table-column>
      <a-table-column key="appName" :width="100" align="center" data-index="appName" title="支付应用"/>
      <a-table-column key="price" :width="100" data-index="price" title="转账金额">
        <template #default="text">
          <span>{{ text | price }}</span>
        </template>
      </a-table-column>
      <a-table-column key="status" :width="100" align="center" data-index="status" title="转账状态">
        <template #default="text">
          <a-tag :color="Status[text].color">{{ Status[text].name }}</a-tag>
        </template>
      </a-table-column>
      <a-table-column key="merchantTransferId" :width="300" align="left" title="订单号">
        <template #default="record">
          <p class="order-font">
            <a-tag size="small">商户</a-tag>
            {{ record.merchantTransferId }}
          </p>
          <p v-if="record.transferNo" class="order-font">
            <a-tag color="orange" size="small">转账</a-tag>
            {{ record.transferNo }}
          </p>
          <p v-if="record.channelTransferNo" class="order-font">
            <a-tag color="green" size="small">渠道</a-tag>
            {{ record.channelTransferNo }}
          </p>
        </template>
      </a-table-column>
      <a-table-column key="userName" :width="120" align="center" data-index="userName" title="收款人姓名"/>
      <a-table-column key="userAccount" :width="200" align="center" data-index="userAccount" title="收款账号"/>
      <a-table-column key="subject" :width="120" align="center" data-index="subject" title="转账标题"/>
      <a-table-column key="channelCode" :width="120" align="center" data-index="channelCode" title="转账渠道">
        <template #default="text">
          <a-tag v-if="text">{{ PayChannelCode[text].name }}</a-tag>
        </template>
      </a-table-column>
      <a-table-column key="successTime" :width="180" data-index="successTime" title="转账成功时间">
        <template #default="text">
          <span v-if="text">{{ text | moment }}</span>
        </template>
      </a-table-column>
      <a-table-column key="action" fixed="right" title="操作">
        <template #default="record">
        <span>
          <a @click="handleDetail(record)">详情</a>
        </span>
        </template>
      </a-table-column>
    </s-table>
    <TransferDetail ref="TransferDetail"/>
  </a-card>
</template>

<script>
import * as Api from '@/api/pay/transfer'
import * as AppApi from '@/api/pay/app'
import {STable} from '@/components/Table'
import TransferDetail from './TransferDetail'
import StatusEnum from '@/common/enum/pay/PayTransferStatusEnum'
import PayChannelCodeEnum from '@/common/enum/pay/PayChannelCodeEnum'

export default {
  name: 'Index',
  components: {
    STable,
    TransferDetail
  },
  data() {
    return {
      // 当前表单元素
      searchForm: this.$form.createForm(this),
      // 查询参数
      queryParam: {},
      // 正在加载
      isLoading: false,
      // 加载数据方法 必须为 Promise 对象
      loadData: param => {
        return Api.page({...param, ...this.queryParam})
            .then(response => response)
      },
      appList: []
    }
  },
  created() {
    // 获取目录列表
    this.getAppList()
  },
  computed: {
    Status() {
      return StatusEnum
    },
    PayChannelCode() {
      return PayChannelCodeEnum
    }
  },
  methods: {
    getAppList() {
      AppApi.list().then(data => {
        this.appList = data.items
      })
    },

    // 详情
    handleDetail(record) {
      this.$refs.TransferDetail.open(record.id)
    },

    /**
     * 刷新列表
     * @param {Boolean} bool 强制刷新到第一页
     */
    handleRefresh(bool = false) {
      this.$refs.table.refresh(bool)
    },

    // 确认搜索
    handleSearch(e) {
      e.preventDefault()
      this.searchForm.validateFields((error, values) => {
        if (!error) {
          this.queryParam = {...this.queryParam, ...values}
          this.handleRefresh(true)
        }
      })
    },

    // 重置搜索表单
    handleReset() {
      this.searchForm.resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
.ant-card-body {
  padding: 22px 29px 25px;
}

.order-font {
  padding: 2px 0;
  font-size: 12px;
}
</style>
